package com.jfeat.am.module.wegou.api.crud;

import com.baomidou.mybatisplus.plugins.Page;
import com.jfeat.am.common.constant.tips.ErrorTip;
import com.jfeat.am.common.constant.tips.SuccessTip;
import com.jfeat.am.common.constant.tips.Tip;
import com.jfeat.am.common.controller.BaseController;
import com.jfeat.am.common.exception.BusinessException;
import com.jfeat.am.module.wegou.exception.WegouBizException;
import com.jfeat.am.module.wegou.services.crud.service.ProductTypeService;
import com.jfeat.am.module.wegou.services.domain.service.QueryProductTypeService;
import com.jfeat.am.module.wegou.services.patch.ProductTypePatchService;
import com.jfeat.am.module.wegou.services.persistence.model.ProductType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 产品类别表 api
 * </p>
 *
 * @author Code Generator
 * @since 2018-01-03
 */
@Api("料理类型")
@RestController
@RequestMapping("/api/product/type")
public class ProductTypeEndpoint extends BaseController {

    @Resource
    ProductTypeService productTypeService;

    @Resource
    QueryProductTypeService queryProductTypeService;

    @Resource
    ProductTypePatchService productTypePatchService;

    @ApiOperation(value = "料理类型 新建料理类型", response = ProductType.class)
    @PostMapping
    public Tip createProductType(@RequestBody ProductType entity) {
        try {
            productTypeService.createGroup(entity);
        } catch (Exception e) {
            throw new BusinessException(4000, "名字重复");
        }
        return SuccessTip.create(entity);
    }

    @ApiOperation(value = "料理类型 查看料理类型", response = ProductType.class)
    @GetMapping("/{id}")
    public Tip getProductType(@PathVariable Long id) {
        return SuccessTip.create(productTypeService.retrieveGroup(id));
    }

    @ApiOperation(value = "料理类型 修改料理类型", response = ProductType.class)
    @PutMapping("/{id}")
    public Tip updateProductType(@PathVariable Long id, @RequestBody ProductType entity) {
        entity.setId(id);
        ProductType originType = productTypeService.retrieveGroup(id);
        //不能作为自己的子类别
        if (originType.getId().equals(entity.getPid())) {
            return ErrorTip.create(WegouBizException.CATEGORY_CANNOT_BE_HIS_OWN_CHILD.getCode(),
                    WegouBizException.CATEGORY_CANNOT_BE_HIS_OWN_CHILD.getMessage());
        }
        productTypeService.updateGroup(entity);
        return SuccessTip.create(entity);
    }

    @ApiOperation(value = "料理类型 删除某个料理类型", response = ProductType.class)
    @DeleteMapping("/{id}")
    public Tip deleteProductType(@PathVariable Long id) {
        if (productTypePatchService.hasChildren(id)) {
            return ErrorTip.create(WegouBizException.CANNOT_DELETE_WITH_CHILDREN.getCode(),
                    WegouBizException.CANNOT_DELETE_WITH_CHILDREN.getMessage());
        }
        if (productTypePatchService.refBySomeProducts(id)) {
            return ErrorTip.create(WegouBizException.CANNOT_DELETE_REF_SOME_PRODUCTS.getCode(),
                    WegouBizException.CANNOT_DELETE_REF_SOME_PRODUCTS.getMessage());
        }
        return SuccessTip.create(productTypeService.deleteGroup(id));
    }

    @ApiOperation(value = "料理类型 查询类型  暂时不需要用到", response = ProductType.class)
    @GetMapping
    public Tip queryProductTypes(Page page,
                                 @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                 @RequestParam(name = "id", required = false) Long id,
                                 @RequestParam(name = "typeName", required = false) String typeName,
                                 @RequestParam(name = "zhTypeName", required = false) String zhTypeName,
                                 @RequestParam(name = "sortNumber", required = false) Integer sortNumber) {
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        ProductType productType = new ProductType();
        productType.setId(id);
        productType.setTypeName(typeName);
        productType.setZhTypeName(zhTypeName);
        productType.setSortNumber(sortNumber);

        page.setRecords(queryProductTypeService.recurisivelyFindProductTypesPage(page, productType));

        return SuccessTip.create(page);
    }

    /**
     * 查询所有类型（不分页）（树形结构）
     */
    @GetMapping("/all")
    public Tip queryAllProductTypes() {
        return SuccessTip.create(queryProductTypeService.recurisivelyFindAllProductTypes());
    }
}
